(function() {

this.codium = this.codium || {};
this.codium.control = this.codium.control || {};

this.codium.control.Tab = pasta.webgui.ContainerControl.extend(
{
	init : function() 
	{
		this._super();
		
		this.lblCaption = new pasta.webgui.Label();
		this.lblCaption.setLeft(0);
		this.lblCaption.setTop(4);
		this.lblCaption.setWidth(this.getWidth());
		this.lblCaption.setAlign("center");
		this.lblCaption.font.setSize(12);
		this.addControl(this.lblCaption);
		
		this.setActive(false);
		this.setButtonMode(true);
	},
	
	setActive : function(data)
	{
		this.active = data;
		
		if (this.active)
		{
			this.setShadow("0px 0px 4px #00000");
			
			this.canvas.style.background = "#FFFFFF";
			this.canvas.style.borderTop = "1px solid #000000";
			this.canvas.style.borderLeft = "1px solid #000000";
			this.canvas.style.borderRight = "1px solid #000000";
			
			this.lblCaption.font.setColor("#000000");
			this.lblCaption.font.setBold(true);
		}
		else
		{
			this.setShadow("");
			
			this.canvas.style.background = "#666666";
			this.canvas.style.borderTop = "1px solid #888888";
			this.canvas.style.borderLeft = "1px solid #888888";
			this.canvas.style.borderRight = "1px solid #888888";
			
			this.lblCaption.font.setColor("#CCCCCC");
			this.lblCaption.font.setBold(false);
		}
	},
	
	setCaption : function(data)
	{
		this.lblCaption.setCaption(data);
	},
	
	setWidth : function(data)
	{
		this._super(data);
		
		this.lblCaption.setWidth(this.getWidth());
	}
});

this.codium.control.Page = pasta.webgui.ContainerControl.extend(
{
	init : function() 
	{
		this._super();
		
		this.setShadow("0px 2px 4px #00000");
		
		this.canvas.style.background = "#FFFFFF";
		this.canvas.style.borderBottom = "1px solid #000000";
		this.canvas.style.borderLeft = "1px solid #000000";
		this.canvas.style.borderRight = "1px solid #000000";
		
		this.tabWidth = 100;
		this.tabCaption = "Page";
	},
	
	getCaption : function()
	{
		return this.tabCaption;
	},
	
	setCaption : function(data)
	{
		this.tabCaption = data;
		
		if (this.tab != null)
			this.tab.setCaption(data);
	},
	
	getTabWidth : function()
	{
		return this.tabWidth;
	},
	
	setTabWidth : function(data)
	{
		this.tabWidth = data;
		
		if (this.tab != null)
			this.tab.setWidth(data);
		
		if (this._onChange != null)
			this._onChange.callHandler(this);
	},	
	
	getOnChange : function()
	{
		if (this._onChange == null)
			this._onChange = new pasta.util.EventObj();
		
		return this._onChange;
	},
});

this.codium.control.PageControl = pasta.webgui.ContainerControl.extend(
{
	init : function() 
	{
		this._super();
		
		this.tabHeight = 25;
		this.defaultTabWidth = 100;
		this.hasChild = false;
	},
	
	addControl : function(child)
	{
		this._super(child);
		
		if (child instanceof codium.control.Page)
		{
			child.setLeft(0);
			child.setTop(this.tabHeight);
			child.setWidth(this.getWidth());
			child.setVisible(false);
			child.setHeight(this.getHeight() - this.tabHeight);
			
			child.tab = new codium.control.Tab();
			child.tab.setWidth(child.getTabWidth());
			child.tab.setCaption(child.getCaption());
			child.tab.page = child;
			child.tab.setHeight(this.tabHeight - 2);
			this.addControl(child.tab);
			
			this.arrangeTab();
			
			child.getOnChange().setHandler(this, this.pageChange);
			child.tab.getOnClick().setHandler(this, this.tabClick);
			
			if (!this.hasChild)
				this.setActivePage(child);
			
			this.hasChild = true;
		}
	},
	
	setActivePage : function(data)
	{
		for (var i = 0; i < this.controls.getLength(); i++)
		{
			var child = this.controls.get(i);
			
			if (child instanceof codium.control.Page)
			{
				if (child == data)
				{
					child.setVisible(true);
					child.tab.setActive(true);
					child.tab.setTop(-3);
					child.tab.setHeight(this.tabHeight + 2);
				}
				else
				{
					child.tab.setActive(false);
					child.setVisible(false);
					child.tab.setTop(0);
					child.tab.setHeight(this.tabHeight - 2);
				}
			}
		}
	},
	
	tabClick : function(sender)
	{
		this.setActivePage(sender.page);
	},
	
	pageChange : function(sender)
	{
		this.arrangeTab();
	},
	
	arrangeTab : function()
	{
		var left = 0;
		
		for (var i = 0; i < this.controls.getLength(); i++)
		{
			var child = this.controls.get(i);
			
			if (child instanceof codium.control.Tab)
			{
				child.setLeft(left);
				
				left += child.getWidth() + 2;
			}
		}
	},
	
	//------------------ Event Handler -----------------
	
	setHeight : function(data)
	{
		this._super(data);
		
		for (var i = 0; i < this.controls.getLength(); i++)
		{
			var child = this.controls.get(i);
			
			if (child instanceof codium.control.Page)
			{
				child.setHeight(this.getHeight() - this.tabHeight);
			}
		}
	},
	
	setWidth : function(data)
	{
		this._super(data);
		
		for (var i = 0; i < this.controls.getLength(); i++)
		{
			var child = this.controls.get(i);
			
			if (child instanceof codium.control.Page)
			{
				child.setWidth(this.getWidth());
			}
		}
	},
});

})();